package com.ws.controller;

import com.ws.entity.*;
import com.ws.service.RoleService;
import com.ws.service.UserService;
import com.ws.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    public UserService userService;

    @Autowired
    public RoleService roleService;


    @PostMapping("/admin/login")
    public Result adminLogin(@RequestBody User user, HttpServletRequest request){

        return userService.adminLogin(user,request);
    }

    @PostMapping("/menu/list/all/role/{roleId}")
    public Result getMenuListByRoleId (@PathVariable(value = "roleId", required = false) Long roleId) {

        return userService.getMenuListByRoleId(roleId);
    }

    @GetMapping("/admin/list/page")
    public Result getAdminListForPage(
            @RequestParam(value = "pno",defaultValue = "1",required = false) int pno,
            @RequestParam(value = "psize",defaultValue = "10",required = false) int psize,
            @RequestParam(value = "username",defaultValue = "",required = false) String username ) {

        return userService.getAdminListForPage(pno, psize,username);
    }

    @GetMapping("/role/list/all")
    public Result getRoleListAll() {
        return userService.getRoleListAll();
    }

    @PutMapping("/admin/insert")
    public Result adminInsert(@RequestBody User user) {
        return userService.adminInsert(user);
    }

    @GetMapping("/admin/find/id/{id}")
    public Result findAdminById(@PathVariable("id") Long id) {
        return userService.findAdminById(id);
    }

    @PutMapping("/admin/update")
    public Result adminUpdate(@RequestBody User user) {
        return userService.adminUpdate(user);
    }

    @DeleteMapping("/admin/delete/id/{id}")
    public Result deleteAdminById(@PathVariable("id") Long id) {
        return userService.deleteAdminById(id);
    }

    @GetMapping("/role/list/page")
    public Result getRoleListForPage(
            @RequestParam(value = "pno",defaultValue = "1",required = false) int pno,
            @RequestParam(value = "psize",defaultValue = "10",required = false) int psize
    ) {

        return roleService.getRoleListForPage(pno,psize);
    }


    @PutMapping("/role/insert")
    public Result roleInsert(@RequestBody Role role) {
        return roleService.roleInsert(role);
    }

    @DeleteMapping("/role/delete/id/{roleId}")
    public Result deleteRoleById(@PathVariable("roleId") Long roleId) {
        return roleService.deleteRoleById(roleId);
    }

    @GetMapping("/role/find/id/{roleId}")
    public Result findRoleById(@PathVariable("roleId") Long roleId) {
        return roleService.findRoleById(roleId);
    }

    @PutMapping("/role/update")
    public Result updateRole(@RequestBody Role role) {
        return roleService.updateRole(role);
    }

    @PostMapping("/admin/password/change")
    public Result changeAdminPassword(@RequestBody User user) {
        return userService.updateAdminPassword(user);
    }

    @GetMapping("/menu/list/all")
    public Result getMenuListALl() {
        return userService.getMenuListALl();
    }

    @PostMapping("/menu/insert/menu-role")
    public Result insertMenuRole(@RequestBody MenuRole menuRole) {
        return userService.insertMenuRole(menuRole);
    }

    @PutMapping("/menu/insert")
    public Result insertMenu(@RequestBody Menu menu) {
        return userService.insertMenu(menu);
    }

    @PutMapping("/menu/insert/pid//{pid}")
    public Result insertMenuChildren(@PathVariable("pid") Long pid,@RequestBody Menu menu) {
        return userService.insertMenuChildren(pid,menu);
    }

    @GetMapping("/menu/find/id/{id}")
    public Result findMenuById(@PathVariable("id") Long id) {
        return userService.findMenuById(id);
    }

    @PutMapping("/menu/update")
    public Result updateMenu(@RequestBody Menu menu) {
        return userService.updateMenu(menu);
    }

    @DeleteMapping("/menu/delete/id/{id}")
    public Result deleteMenu(@PathVariable("id") Long id) {
        return userService.deleteMenu(id);
    }

    @PostMapping("/shop/login/password")
    public Result loginPassword(@RequestBody User user) {
        return userService.loginPassword(user);
    }

    @PostMapping("/shop/register")
    public Result registerUser(@RequestBody User user) {
        return userService.registerUser(user);
    }


    @PutMapping("/shop/update")
    public Result updateShopAdmin(@RequestBody User user) {
        return userService.adminUpdate(user);
    }

    @GetMapping("/address/list/page")
    public Result getAddressListForPage(
            @RequestParam(value = "pno",defaultValue = "1",required = false) int pno,
            @RequestParam(value = "psize",defaultValue = "5",required = false) int psize){
        return userService.getAddressListForPage(pno, psize);
    }

    @PutMapping("/address/insert")
    public Result insertAddress(@RequestBody Address address) {
        return userService.insertAddress(address);
    }

    @GetMapping("/address/find/id/{id}")
    public Result findAddressById(@PathVariable("id") Long addressId) {
        return userService.findAddressById(addressId);
    }

    @PutMapping("/address/update")
    public Result updateAddress(@RequestBody Address address) {
        return userService.updateAddress(address);
    }

    @DeleteMapping("/address/delete/id/{id}")
    public Result deleteAddress(@PathVariable("id") Long addressId) {
        return userService.deleteAddress(addressId);
    }









}
